import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { DepartmentService } from '../services/department.service';
import { Department } from '../models/department.entity';

@Controller('departments')
export class DepartmentController {
  constructor(private readonly departmentService: DepartmentService) {}

  @Get()
  async getDepartments(): Promise<Department[]> {
    return this.departmentService.findAll();
  }

  @Get(':id')
  async getDepartment(@Param('id') id: string): Promise<Department | null> {
    return this.departmentService.findOne(Number(id));
  }

  @Post()
  @UsePipes(new ValidationPipe())
  async createDepartment(
    @Body() deptData: Partial<Department>,
  ): Promise<Department> {
    return this.departmentService.create(deptData);
  }

  @Put(':id')
  @UsePipes(new ValidationPipe())
  async updateDepartment(
    @Param('id') id: string,
    @Body() deptData: Partial<Department>,
  ): Promise<Department | null> {
    return this.departmentService.update(Number(id), deptData);
  }

  @Delete(':id')
  async deleteDepartment(@Param('id') id: string): Promise<Department | null> {
    return this.departmentService.remove(Number(id));
  }
}
